package lab;

import java.io.*;
import java.util.Iterator;

import java.util.zip.DataFormatException;

public class Programm {
	
	BufferedReader input;
    PrintWriter output;
    boolean reCreated = false;
	
    public void execute() throws DataFormatException, IOException, NumberFormatException{
    	
    	CloseHashSet table = new CloseHashSet(5);
    	Iterator<Integer> iter = table.iterator();
        String temp = input.readLine();
        
        while(temp != null){
        	String [] splitted = temp.split(" ");
        	switch(splitted[0]) {  
        		case "add":
        			try {
                		int value = Integer.parseInt(splitted[1]);
                		table.add(value);
                	}
        			catch (IllegalArgumentException e) {
                        throw new IllegalArgumentException(e);
                    }
                	break;
                	
                case "remove":
                	try {
                		int value = Integer.parseInt(splitted[1]);
                		output.println(table.remove(value));
                	}
                	catch (IllegalArgumentException e) {
                        throw new IllegalArgumentException("invalid argument");
                    }
                	break;
                	
                case "contains":
                	try {
                		int value = Integer.parseInt(splitted[1]);
                		output.println(table.contains(value));
                	}
                	catch (IllegalArgumentException e) {
                        throw new IllegalArgumentException("invalid argument");
                    }
                	break;
                	
                case "toString":
                	output.println(table.toString());
                	break;
                	
                case "clear":
                	table.clear();
                    break;
                    
                case "isEmpty":
                	output.println(table.isEmpty());
                    break;
                    
                case "size":
                	output.println(table.size());
                    break;
                    
                case "next":
                	if(!reCreated) {                		
                		iter = table.iterator();
                		reCreated = true;
                	}
                    output.println(iter.next());
                    break;
                    
                default:
                	output.println("Not correct command!");
                }   
                temp=input.readLine();
        }   
	}
    
	public void run(String fileNameIn, String fileNameOut) throws DataFormatException, IOException, FileNotFoundException{
		try{
        	input = new BufferedReader(new FileReader(fileNameIn));
        	output = new PrintWriter(fileNameOut);
        	execute();
        } catch(Exception e) {
        	System.err.println(e);
        }
        finally {
        	try {
        		if (input != null) input.close();
        		if (output != null) output.close();
        		if (output.checkError()) {
        			throw new IOException();
        		}
        	}
        	catch(IOException e){
        		System.err.println("IO exception!");
        	}
        }
	}
	
	public static void main(String[] args)  throws DataFormatException, IOException {

		if(args.length < 1){
			throw new DataFormatException("No filenames in args!");
		}
		else if(args.length == 1){
			throw new DataFormatException("No output filename in args");
		}
	    else if(args.length > 2){
	    	throw new DataFormatException("Too much args!");
	    }
	    else{
	    	new Programm().run(args[0], args[1]);
	    }
	}
}